package wu.example.orderserver.controller;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wu.example.orderserver.spi.AuthService;

import java.util.List;

/**
 * @author benjamin_5
 * @Description
 * @date 2024/8/29
 */
@RestController
@RequestMapping("spi")
public class SpiController {

    @GetMapping("getInfo")
    public String getInfo(String type, String group){
        ExtensionLoader<AuthService> extensionLoader =
                ExtensionLoader.getExtensionLoader(AuthService.class);
//        AuthService service = extensionLoader.getExtension(type);

        URL url = URL.valueOf("localhost:8081/getInfo?name="+type);
        List<AuthService> activateExtension = extensionLoader.getActivateExtension(url, "id", group);
        String info = "";
        for (AuthService authService : activateExtension) {
            info += authService.getInfo() + "\n";
        }

//        return service.getInfo();
        return info;
    }

    @GetMapping("getInfo2")
    public String getInfo2(String type){
        ExtensionLoader<AuthService> extensionLoader =
                ExtensionLoader.getExtensionLoader(AuthService.class);
        AuthService service = extensionLoader.getExtension(type);

        URL url = URL.valueOf("localhost:8081/order?id=1");
        List<AuthService> activateExtension = extensionLoader.getActivateExtension(url, type);


        return service.getInfo();
    }

}
